package ext.tianma.bom.service.rdpInterface;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wt.method.RemoteAccess;
import wt.session.SessionServerHelper;

/**
 * 类名: RDPServcie </br>
 * 包名: ext.tianma.integration
 * 描述: PLM提供接口，供RDP调用
 * @author xucr
 * 创建时间: 2020年10月19日
 * 发布版本: V1.0
 */
@Controller
@RequestMapping({ "/RDPController" })
public class RDPController implements RemoteAccess {
	
	@Autowired
	RDPService rdpservice;

	private static final Logger logger = LoggerFactory.getLogger(RDPController.class);

	/**
	 * RDP接口调用入口
	 * @param param
	 * @param response
	 * @throws JSONException
	 * @author xucr
	 * @CreateDate 2020年10月12日
	 */
	@RequestMapping(value = "/getPLMData", method = {RequestMethod.POST, RequestMethod.GET })
	public void getPLMData(
			@RequestParam(value = "param", required = false) String param,
			HttpServletResponse response) throws JSONException {
		logger.info("----调用接口传递参数：" + param);
		response.setContentType("text/html;charset=UTF-8");
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject result = new JSONObject();
			PrintWriter out = response.getWriter();
			String businessType = "";
			if(StringUtils.isNotEmpty(param)) {
				JSONObject paramObject = new JSONObject(param);
				if(paramObject.has("businessType")) {
					businessType = paramObject.getString("businessType") ;
				}
				
				//获取BOM结构
				if("getBOMFile".equals(businessType)) {
					result = rdpservice.getBOMFile(paramObject);
				}
				
				//获取BOM指定半成集合
				if("getSemiFinishedGoodByBOM".equals(businessType)) {
					result = rdpservice.getSemiFinishedGoodByBOM(paramObject);
				}
			}
			if(StringUtils.isEmpty(businessType)) {
				result.put(RDPService.SUCCESS, false);
				result.put(RDPService.CODE, RDPService.CODE_FAILED);
				result.put(RDPService.MSG, "缺少参数：businessType");
			}
			out.print(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
}
